package com.example.batch.writer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.file.MultiResourceItemWriter;
import org.springframework.batch.item.file.ResourceSuffixCreator;
import org.springframework.batch.item.file.SimpleResourceSuffixCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

import java.util.List;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/7/12 09:39
 */
@Component
@Slf4j
public class MyMultiResourceFlatFileItemWriter<String> extends MultiResourceItemWriter {

    public static final java.lang.String filaPath = "/Users/yuhang/Downloads/multi-result";


    @Autowired
    public MyMultiResourceFlatFileItemWriter(MyFlatFileItemWriter writer) {
        setResource(new PathResource(filaPath));
        //每个文件多少条记录
        setItemCountLimitPerResource(2000);
        setResourceSuffixCreator(new SimpleResourceSuffixCreator());
        setSaveState(true);
        setDelegate(writer);
        setResourceSuffixCreator(new ResourceSuffixCreator() {
            @Override
            public java.lang.String getSuffix(int index) {
                return "." + index + ".txt";
            }
        });
    }

    @Override
    public void write(List items) throws Exception {
        log.info("items.size {}",items.size());
        super.write(items);
    }
}
